﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VideoServer.Helpers;
using VideoServer.ViewModel.Helpers;

namespace VideoServer.ViewModel
{
    public class LogOutputViewModel:ViewModelBase
    {
        private RelayCommand _clearLogCommand;
        private StringBuilder logTextBuilder = new StringBuilder("欢迎使用\r\n");
        private int textLineCount = 0;

        public RelayCommand ClearLogCommand
        {
            get
            {
                if (_clearLogCommand == null)
                    _clearLogCommand = new RelayCommand(param=>ClearLog());
                return _clearLogCommand;
            }
        }

        public string LogText
        {
            get
            {
                return logTextBuilder.ToString();
            }
        }

        private void ClearLog()
        {
            logTextBuilder.Clear();
            OnPropertyChanged("LogText");
            textLineCount = 0;
        }
      
        public void PrintLog(Object sender,CustomEventArgs e)
        {
            logTextBuilder.AppendFormat("{0:HH:mm:ss} {1}: {2}\r\n", DateTime.Now, e.MessageType.ToString(), e.Message);
            OnPropertyChanged("LogText");
            textLineCount++;
            if (textLineCount == 100)
            {
                ClearLog();
            }
        }
    }
}
